# -*- coding: UTF-8 -*-
# Author: Damon(wuud1)
# CreateDate: 
# Message:
from base_class import BaseClass
class GetCarsGroupCollection(BaseClass):

    def __init__(self, **kwargs):
        self.src_db = kwargs.get("src_db")
        self.dest_db = kwargs.get("dest_db")
        self.insert_table = kwargs.get("insert_table")
        self.src_conn, self.src_curs = self.get_conn_curs(db_config=self.src_db, type='dict')
        self.dest_conn, self.dest_curs = self.get_conn_curs(db_config=self.dest_db, type='dict')
        self.num = 0

        self.num_label = {
            "ENGINE MECHANICAL":"01",
            "FUEL & ENGINE CONTROL":"02",
            "EXHAUST & COOLING":"03",
            "ENGINE ELECTRICAL":"04",
            "BODY ELECTRICAL":"05",
            "POWER TRAIN":"06",
            "AXLE & SUSPENSION":"07",
            "BRAKE":"08",
            "STEERING":"09",
            "BODY(FRONT,ROOF & FLOOR)":"10",
            "BODY(SIDE & REAR)":"11",
            "SEAT & SEAT BELT":"12",
            "MISCELLANEOUS":"13",
            "BODY(BACK DOOR & REAR BODY)":"14",
        }
        self.cn = {
            "ENGINE MECHANICAL":"发动机",
            "FUEL & ENGINE CONTROL":"燃料与发动机控制",
            "EXHAUST & COOLING":"排气和冷却",
            "ENGINE ELECTRICAL":"发动机电气",
            "BODY ELECTRICAL":"车身电气",
            "POWER TRAIN":"传动系",
            "AXLE & SUSPENSION":"轴与悬挂",
            "BRAKE":"制动",
            "STEERING":"转向装置",
            "BODY(FRONT,ROOF & FLOOR)":"车身（前、顶、地板）",
            "BODY(SIDE & REAR)":"车身（侧、后）",
            "SEAT & SEAT BELT":"座椅和安全带",
            "MISCELLANEOUS":"杂项",
            "BODY(BACK DOOR & REAR BODY)":"车身（后门和后部）",
        }

    def get_cid_list(self):
        sql = '''select distinct code from cars_info_collection'''
        self.src_curs.execute(sql)
        data = self.src_curs.fetchall()
        return [i.get("code") for i in data]

    def get_info(self, cid):
        sql = '''select * from genloc where trim(f2)="{}";'''.format(cid)
        self.src_curs.execute(sql)
        data = self.src_curs.fetchall()
        return [i.get("f4") for i in data]

    def run(self):
        # 获取所有cidlist, 遍历并获取主组信息
        cid_list = self.get_cid_list()
        for cid in cid_list:
            label_list = self.get_info(cid)
            for label in label_list:
                label = label.strip()
                cid=cid.strip()
                num = self.num_label[label]
                md5str = (cid+str(num)).encode('utf8')
                uid = self.get_md5(md5str)
                _dict = dict(
                    uid=uid,
                    cid=cid,
                    num=num,
                    brandCode="nissan",
                    label=self.cn[label],
                    extra_info='',
                    updatetime='now()',
                    createtime='now()'
                )
                insert_sql = self.construct_insert_sql(self.insert_table, _dict)
                # print(insert_sql)
                self.dest_curs.execute(insert_sql)
                self.num += 1
                if self.num % 100 == 0:
                    self.dest_conn.commit()
                    print("提交insert缓存, 当前数量: {}".format(self.num))
        self.dest_conn.commit()
        print("提交最终缓存, 总数据量: {}".format(self.num))

if __name__ == '__main__':
    src_db = dict(
        host="127.0.0.1",
        port=3306,
        user="root",
        passwd="123456",
        db="nissan_new",
        charset="utf8",
    )
    dest_db = dict(
        host="127.0.0.1",
        port=3306,
        user="root",
        passwd="123456",
        db="nissan_new",
        charset="utf8",
    )
    insert_table = 'cars_group_collection'
    tools = GetCarsGroupCollection(src_db=src_db, dest_db=dest_db,insert_table=insert_table)
    tools.run()


